package com.tusuo.stds.ui.questionbank;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.Constants;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.ApiResult;
import com.tusuo.stds.rest.Question;
import com.tusuo.stds.rest.QuestionBankContent;
import com.tusuo.stds.rest.QuestionBankGroup;
import com.tusuo.stds.rest.WrongQuestionAnswerResult;
import com.tusuo.stds.rest.WrongQuestionSetting;
import com.tusuo.stds.util.DESedeUtil;
import com.tusuo.stds.util.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.WeakHashMap;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;
import timber.log.Timber;

/**
 * Created by developer on 17-6-14.
 */


@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(QuestionBankTempPresenter.class)
public class QuestionBankTempPresenter extends MvpBasePresenter<QuestionBankTempView> {


    Context mContext;
    GetQuestionBankGroupSubscriber mGetQuestionBankGroupSubscriber;
    GetWrongQuestionSettingSubscriber mGetWrongQuestionSettingSubscriber;

    @Inject
    ApiHelper mApiHelper;
    @Inject
    SharedPreferences mSharedPref;

    private String mErrMsgServer;

    QuestionBankGroup mQuestionBankGroup;

    QuestionBankContent mQuestionBankContent;

    ArrayList<Integer> mList;

    private int mQuestionSettingCorrectCount = 5;
    private int questionSettingCorrectCount = 5;
    private SubmitWrongQuestionAnswerSubscriber mSubmitWrongQuestionAnswerSubscriber;

    QuestionBankTempPresenter(Context context) {

        mContext = context;

        mQuestionBankContent = new QuestionBankContent();

        Injector.initQuestionBankTempPresenterComponent().inject(this);

        mGetQuestionBankGroupSubscriber = new GetQuestionBankGroupSubscriber();
        mApiHelper.getQuestionBankGroup(mGetQuestionBankGroupSubscriber);

        mGetWrongQuestionSettingSubscriber = new GetWrongQuestionSettingSubscriber();
        mApiHelper.getWrongQuestionSetting(mGetWrongQuestionSettingSubscriber);

    }

    @Override
    public void attachView(QuestionBankTempView view) {
        super.attachView(view);
        if (getView() != null) {
            getView().initFirst();
            if (mSharedPref == null) {
                mSharedPref = PreferenceManager.getDefaultSharedPreferences(mContext);
            }
            getView().setSecretKey(DESedeUtil.getSecretKey(mSharedPref.getString(Constants.KEY_COOKIE, "")));
        }
    }

    public int getQuestionSettingCorrectCount() {
        return mQuestionSettingCorrectCount;
    }

    public void setQuestionSettingCorrectCount(int questionSettingCorrectCount) {
        this.mQuestionSettingCorrectCount = questionSettingCorrectCount;

        mApiHelper.setWrongQuestionSetting(new SetWrongQuestionSettingSubscriber(questionSettingCorrectCount), questionSettingCorrectCount);

    }

    public void removeWrongQuestion(int id) {

        mApiHelper.removeWrongQuestion(new RemoveWrongQuestionSubscriber(id), id);

    }

    public void submitWrongQuestionAnswer(String answerContent, int id) {
        mSubmitWrongQuestionAnswerSubscriber = new SubmitWrongQuestionAnswerSubscriber(id, answerContent);
        mApiHelper.submitWrongQuestionAnswer(mSubmitWrongQuestionAnswerSubscriber,
                id,
                answerContent);
    }

    public void getQuestionBankContent(String ids) {
        mApiHelper.getQuestionBankContent(new QuestionBankTempPresenter.GetQuestionBankContentSubscriber(), ids);
    }

    private class SubmitWrongQuestionAnswerSubscriber extends Subscriber<WrongQuestionAnswerResult> {

        private int mWrongQuestionId;
        private String mAnswerContent;

        public SubmitWrongQuestionAnswerSubscriber(int wrongQuestionId, String answerContent) {
            mWrongQuestionId = wrongQuestionId;
            mAnswerContent = answerContent;
        }

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final WrongQuestionAnswerResult result) {
            if (getView() != null) {
                getView().toNextQuestion(result.CorrectCount,result.WrongCount);
            }
        }
    }


    private class SetWrongQuestionSettingSubscriber extends Subscriber<ApiResult> {

        private int mNumber;

        public SetWrongQuestionSettingSubscriber(int number) {
            mNumber = number;
        }

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final ApiResult result) {
            if (result.checkState(mContext)) {
                mQuestionSettingCorrectCount = mNumber;
            }
        }
    }

    private class RemoveWrongQuestionSubscriber extends Subscriber<ApiResult> {
        private int mWrongQuestionId;

        public RemoveWrongQuestionSubscriber(int wrongQuestionId) {
            mWrongQuestionId = wrongQuestionId;
        }

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final ApiResult result) {
            if (result.checkState(mContext)) {
            }
        }
    }

    private class GetWrongQuestionSettingSubscriber extends Subscriber<WrongQuestionSetting> {
        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final WrongQuestionSetting wrongQuestionSetting) {
            if (wrongQuestionSetting.checkState(mContext)) {
                mQuestionSettingCorrectCount = wrongQuestionSetting.CorrectCount;
                if (getView() != null) {
                    getView().setQuestionSetting(mQuestionSettingCorrectCount);
                }
            }
        }
    }

    private class GetQuestionBankGroupSubscriber extends Subscriber<QuestionBankGroup> {
        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final QuestionBankGroup questionBankGroup) {
            if (questionBankGroup.checkState(mContext)) {
                mQuestionBankGroup = questionBankGroup;
                if (getView() != null) {
                    getView().setQuestionBankGroup(questionBankGroup);
                }

                for (int i = 0; i < mQuestionBankGroup.Total; i++) {
                     mQuestionBankContent.Total += mQuestionBankGroup.Data.get(i).List.size();
                }

            }
        }
    }

    private class GetQuestionBankContentSubscriber extends Subscriber<QuestionBankContent> {

        private boolean mFinalRequest;
        private boolean mDataOrder;
        private int mIndex;

        public GetQuestionBankContentSubscriber(boolean finalRequest, boolean dataOrder, int i) {
            mFinalRequest = finalRequest;
            mDataOrder = dataOrder;
            mIndex = i;
        }

        public GetQuestionBankContentSubscriber() {

        }

        @Override
        public void onCompleted() {
//            if (getView() != null) {
//                if (mFinalRequest) {
//                    getView().initAdapter(mQuestionBankContent.Data);
//                }
                /*else {
                    getView().loadOrder(mIndex);
                }*/
//            }


            /*if (mFinalRequest && getView() != null) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            if (mQuestionBankContent.Data.size() < mQuestionBankContent.Total) {
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                getView().initAdapter(mQuestionBankContent.Data);
                                break;
                            }
                        }
                    }
                }).start();
            }*/
        }

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final QuestionBankContent questionBankContent) {

            if (getView() != null && questionBankContent.Total > 0) {
                getView().initAdapter(questionBankContent.Data);
            }else {
                Utils.UI.showToastShort(mErrMsgServer);
            }

        }
    }
}
